package com.feib.soeasy.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;

import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.Payment;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.GroupService;
import com.feib.soeasy.service.PaymentService;

/**
 * @title (#)Soez1004ActionBean.java <br>
 * @description 一般帳單繳費進度查詢 Controller<br>
 * @author Leo Lee
 * @version 1.0.0 2010/11/28
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/28 create by Leo Lee<br>
 */
public class Soez1004ActionBean extends BaseActionBean implements ValidationErrorHandler {

    /**
     * 學雜費繳費作業畫面
     */
    private static final String DEFAULT_JSP = "/WEB-INF/views/soez1004/soez1004.jsp";

    /**
     * 學雜費繳費清單畫面
     */
    private static final String PAYMENT_LIST_JSP = "/WEB-INF/views/soez1004/soez1004List.jsp";
    /**
     * 檔案內容
     */
    private static final String AJAX_QUERY_PAYMENT_JSP = "/WEB-INF/views/soez1004/paymentList.jsp";


    private String groupNo;
    private String queryId;
    private Long paymentPk;

    @SpringBean("paymentService")
    private PaymentService paymentService;

    @SpringBean("groupService")
    private GroupService groupService;


    /**
     * 查詢結果
     */
    private PaginatedList<Payment> queryResult;
    /**
     * 查詢單筆Payment
     */
    private Payment resultPayment;


    @DontBind
    @DefaultHandler
    public Resolution doDefault()
    {       
        logger.debug("Enter doDefault mehtod.");
        return new ForwardResolution(DEFAULT_JSP); 
    }

    /**
     * 查詢
     * @return
     */
    public Resolution doAjaxQuery()
    {
        logger.debug("Enter doAjaxQuery mehtod.");
        try {
            Payment qryPayment = new Payment();
            
            Group group= new Group();  
            group.setGroupNo(groupNo);
   
            
            qryPayment.setPayerId(queryId);

            qryPayment.setGroup(group);
            queryResult.setSortCriterion("virtualAccount");
            queryResult.setSortDirection(1);           
            queryResult.setPageSize(999);
            queryResult = paymentService.queryByPayment(
                            qryPayment, null, null, queryResult, "Q");

        }
        catch (Exception e) {
            logger.error("一般帳單繳費進度查詢錯誤(Soez1004ActionBean.doAjaxQuery):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(PAYMENT_LIST_JSP);
    }

    /**
     * 查詢轉入的帳單資料
     * @return
     */
    public Resolution doAjaxQueryFilePayments()
    {
        logger.debug("Enter doAjaxQueryFilePayments mehtod.");
        try {

            //呼叫服務查詢
            Payment qryPayment = new Payment();
            qryPayment.setPaymentPk(paymentPk);
            resultPayment = 
                paymentService.getPaymentByPk(qryPayment);
        }
        catch (Exception e) {
            logger.error("一般帳單繳費進度查詢-帳單內容查詢錯誤(Soez1004ActionBean.doAjaxQueryFilePayments):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(AJAX_QUERY_PAYMENT_JSP);
    }


    public PaginatedList<Payment> getQueryResult() {
        return queryResult;
    }

    public void setQueryResult(PaginatedList<Payment> queryResult) {
        this.queryResult = queryResult;
    }


    public String getGroupNo() {
        return groupNo;
    }

    public void setGroupNo(String groupNo) {
        this.groupNo = groupNo;
    }

    public String getQueryId() {
        return queryId;
    }

    public void setQueryId(String queryId) {
        this.queryId = queryId;
    }

    public Long getPaymentPk() {
        return paymentPk;
    }

    public void setPaymentPk(Long paymentPk) {
        this.paymentPk = paymentPk;
    }

    public Payment getResultPayment() {
        return resultPayment;
    }

    public void setResultPayment(Payment resultPayment) {
        this.resultPayment = resultPayment;
    }





}
